/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import Entity.Address;
import Entity.Customer;
import Entity.SystemUser;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author cheeseng
 */
@Stateless
public class CustomerSession implements CustomerSessionLocal {

    @PersistenceContext   
    private EntityManager em;

    @Override
    public List<Customer> getCustomerByType(String customerType) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean editCustomer() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public Long createCustomer(Long leadId, String salutation, String contactPerson, String company, String comAddress1, String comAddress2, String comPostalCode, String comCountry, String phone, String email, String description, String billingAddress1, String billingAddress2, String billingPostalCode, String billingCountry, String shippingAddress1, String shippingAddress2, String shippingPostalCode, String shippingCountry, String customerType, Long userId) {
        {
            //make a check if duplicate customer exist


            //if no duplicate do conversion
            Customer c = em.find(Customer.class, leadId);
            if (c != null) {

                c.setCompany(company);
                Address companyAddress = c.getCompanyAddress();
                companyAddress.setAddress1(comAddress1);
                companyAddress.setAddress2(comAddress2);
                companyAddress.setpostalCode(comPostalCode);
                companyAddress.setCountry(comCountry);
                c.setCompanyAddress(companyAddress);
                c.setContactPerson(contactPerson);
                c.setDescription(description);
                c.setEmail(email);
                c.setPhone(phone);
                c.setSalutation(salutation);


                Address billingAddress = new Address();
                billingAddress.setAddress1(billingAddress1);
                billingAddress.setAddress2(billingAddress2);
                billingAddress.setpostalCode(billingPostalCode);
                billingAddress.setCountry(billingCountry);
                c.setBillingAddress(billingAddress);

                Address shippingAddress = new Address();
                shippingAddress.setAddress1(shippingAddress1);
                shippingAddress.setAddress2(shippingAddress2);
                shippingAddress.setpostalCode(shippingPostalCode);
                shippingAddress.setCountry(shippingCountry);
                c.setShippingAddress(shippingAddress);

                c.setCustomerType(customerType);

                SystemUser u = em.find(SystemUser.class, userId);
                c.setSystemUser(u);
                em.persist(c);
                em.flush();

                return c.getLeadId();
            } else {
                Customer cNew = new Customer();
                cNew.setCompany(company);
                Address companyAddress = cNew.getCompanyAddress();
                companyAddress.setAddress1(comAddress1);
                companyAddress.setAddress2(comAddress2);
                companyAddress.setpostalCode(comPostalCode);
                companyAddress.setCountry(comCountry);
                cNew.setCompanyAddress(companyAddress);
                cNew.setContactPerson(contactPerson);
                cNew.setDescription(description);
                cNew.setEmail(email);
                cNew.setPhone(phone);
                cNew.setSalutation(salutation);


                Address billingAddress = new Address();
                billingAddress.setAddress1(billingAddress1);
                billingAddress.setAddress2(billingAddress2);
                billingAddress.setpostalCode(billingPostalCode);
                billingAddress.setCountry(billingCountry);
                cNew.setBillingAddress(billingAddress);

                Address shippingAddress = new Address();
                shippingAddress.setAddress1(shippingAddress1);
                shippingAddress.setAddress2(shippingAddress2);
                shippingAddress.setpostalCode(shippingPostalCode);
                shippingAddress.setCountry(shippingCountry);
                cNew.setShippingAddress(shippingAddress);

                cNew.setCustomerType(customerType);

                SystemUser u = em.find(SystemUser.class, userId);
                cNew.setSystemUser(u);
                em.persist(cNew);
                em.flush();

                return cNew.getLeadId();
            }
        }
    }
}
